<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/bootstrappaginator/bootstrap-paginator.min.js"></script>
    <script src="./libs/template-web.js"></script>
    <script src="./libs/http.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <!-- <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option> -->
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="page">
                <ul class="pagination"></ul>
            </div>
        </div>
    </div>
    <!-- 下拉菜单模板 -->
    <script type="text/html" id="category_list">
        <option value=''>所有分类</option>
        {{ each data }}
            <option value="{{$value.id}}">{{ $value.name }}</option>
        {{ /each }}
    </script>
    <!-- 文章列表模板 -->
    <script type="text/html" id="article_list">
        {{ each data.data v }}
        <tr>
            <td>{{ v.title }}</td>
            <td>{{ v.author }}</td>
            <td>{{ v.category }}</td>
            <td class="text-center">{{ v.date }}</td>
            <td class="text-center">{{ v.state }}</td>
            <td class="text-center">
                <a href="article_edit.html?id={{ v.id }}" data-id="{{ v.id }}" class="btn btn-default btn-xs">编辑</a>
                <a href="javascript:void(0);" data-id="{{ v.id }}" class="btn btn-danger btn-xs delete">删除</a>
            </td>
        </tr>
        {{ /each }}
    </script>
    <script>
        //入口函数
        $(function () {
            var cPage = 1
            //1.页面一加载：请求分类列表渲染到下拉菜单
            function initcate() {
                $.ajax({
                    url: BigNew.category_list,
                    type: 'get',
                    dataType: 'json',
                    success: function (backData) {
                        console.log(backData);
                        //模板引擎渲染页面 
                        $('#selCategory').html(template('category_list', backData));
                    }
                });
            }
            initcate()

            $('#btnSearch').click(function (e) {
                //阻止表单默认行为
                e.preventDefault();
                cPage = 1
                //2.2 ajax请求
                init()
            });
            function init() {
                console.log("-----" + cPage)
                $.ajax({
                    url: BigNew.article_query,
                    type: 'get',
                    dataType: 'json',
                    data: {
                        page: cPage,
                        perpage: 10,//每页返回10条数据
                        type: $('#selCategory').val(),
                        state: $('#selStatus').val(),
                    },
                    success: function (backData) {
                        console.log(backData);
                        //2.3 模板引擎渲染页面
                        $('.table>tbody').html(template('article_list', backData));
                        setPage(cPage, backData.data.totalPage)
                    }
                });
            }
            init()

            function setPage(pageCurrent, pageSum, callback) {
                $(".pagination").bootstrapPaginator({
                    //设置版本号
                    bootstrapMajorVersion: 3,
                    // 显示第几页
                    currentPage: pageCurrent,
                    // 总页数
                    totalPages: pageSum,
                    //当单击操作按钮的时候, 执行该函数, 调用ajax渲染页面
                    onPageClicked: function (event, originalEvent, type, page) {
                        console.log(page)
                        console.log($('#selCategory').val())
                        // 把当前点击的页码赋值给currentPage, 调用ajax,渲染页面
                        cPage = page
                        init && init()
                    }
                })
            }
            $('table>tbody').on('click', '.delete', function () {
                $.ajax({
                    url: BigNew.article_delete,
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: $(this).attr('data-id')
                    },
                    success: function (backData) {
                        if (backData.code == 204) {
                            alert('删除成功');
                            // window.location.reload();
                            init()
                        } else {
                            alert(backData.msg);
                        }
                    }
                });
            });

            $('#release_btn').click(function () {
                //$() :第一个参数：选择器  第二个参数：document，默认是当前窗口document
                $('.level02>li:eq(1)', window.parent.document).addClass('active')
                    .siblings()
                    .removeClass('active');
            });

        })
    </script>
</body>

</html>